//
//  JZMBProgressHUD_Extended.m
//

#import "MBProgressHUD_Extended.h"

#define ENABLE_UE true


@implementation UIView(MBProgressHUD_Extended)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

- (MBProgressHUD *)ShowProgress
{
    return [self ShowProgress:nil userInteractiion:ENABLE_UE];
}


- (MBProgressHUD *)ShowProgress:(bool)userInteractiion
{
    return [self ShowProgress:nil userInteractiion:userInteractiion];
}

- (MBProgressHUD *)ShowHorizontalBarProgress:(NSString *)message progress:(CGFloat)progress{
    MBProgressHUD * hub = [MBProgressHUD HUDForView:self];
    hub.mode = MBProgressHUDModeAnnularDeterminate;
    if (!hub) {
        hub = [[MBProgressHUD alloc]initWithView:self];
        hub.label.font = [UIFont systemFontOfSize:13];
        hub.removeFromSuperViewOnHide = YES;
        hub.userInteractionEnabled = true;
        [self addSubview:hub];
        [hub showAnimated:true];
        [hub hideAnimated:true afterDelay:MAXFLOAT];
    }
    hub.progress = progress;
    hub.label.text = message;
    return hub;
}

- (MBProgressHUD *)ShowHorizontalBarProgress:(NSString *)message {
    [self removeProgressImmediately];
    MBProgressHUD * hub = [MBProgressHUD HUDForView:self];
    if (!hub) {
        hub = [[MBProgressHUD alloc]initWithView:self];
    }
    hub.mode = MBProgressHUDModeAnnularDeterminate;
//    hub.layer.opacity = .8f;
    hub.label.text = message;
    hub.label.font = [UIFont systemFontOfSize:13];
    hub.removeFromSuperViewOnHide = YES;
    hub.userInteractionEnabled = true;
    [self addSubview:hub];
    [hub showAnimated:true];
    [hub hideAnimated:true afterDelay:MAXFLOAT];
    return hub;
}




- (MBProgressHUD *)ShowProgress:(NSString *)message userInteractiion:(bool)userInteractiion delay:(float)seconds
{
    [self removeProgressImmediately];
    
    MBProgressHUD * hub = [MBProgressHUD HUDForView:self];
    if (!hub) {
        hub = [[MBProgressHUD alloc]initWithView:self];
    }
    hub.mode=MBProgressHUDModeCustomView;
    UIActivityIndicatorView *indicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [indicator startAnimating];
    hub.customView = indicator;
    hub.layer.opacity = .8f;
    hub.label.text = message;
    hub.label.font = [UIFont systemFontOfSize:13];
    hub.removeFromSuperViewOnHide = YES;
    hub.userInteractionEnabled = userInteractiion;
    [self addSubview:hub];
    [hub showAnimated:true];
    [hub hideAnimated:true afterDelay:seconds];
    return hub;
}

- (MBProgressHUD *)showProgress:(NSString *)message userInteractiion:(bool)userInteractiion delay:(float)seconds delegate:(id<MBProgressHUDDelegate>)delegate tag:(int)tag
{
    MBProgressHUD * hub = [MBProgressHUD HUDForView:self];
    if (!hub) {
        hub = [[MBProgressHUD alloc]initWithView:self];
    }
    hub.mode=MBProgressHUDModeCustomView;
    UIActivityIndicatorView *indicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [indicator startAnimating];
    hub.customView = indicator;
    hub.layer.opacity = .8f;
    hub.margin = 2;
    hub.tag = tag;
    hub.label.text = message;
    hub.delegate = delegate;
    hub.removeFromSuperViewOnHide = YES;
    hub.userInteractionEnabled = userInteractiion;
    [self addSubview:hub];
    [hub showAnimated:true];
    [hub hideAnimated:true afterDelay:seconds];
    return hub;
}

- (MBProgressHUD *)ShowProgressMessage:(NSString *)message
{
    return [self ShowProgress:message userInteractiion:ENABLE_UE];
}

- (MBProgressHUD *)ShowProgress:(NSString *)message userInteractiion:(bool)userInteractiion
{
    return [self ShowProgress:message userInteractiion:userInteractiion delay:MAXFLOAT];
}

- (MBProgressHUD *)ShowProgressOnlyLabel:(NSString *)message
{
    [self removeProgressImmediately];
    return [self ShowProgressOnlyLabel:message delay:3.0f];
}


- (MBProgressHUD *)ShowProgressOnlyLabel:(NSString *)message delay:(float)seconds
{
    MBProgressHUD * hub = [MBProgressHUD HUDForView:self];
    if (!hub) {
        hub = [[MBProgressHUD alloc]initWithView:self];
    }
    hub.detailsLabel.text = message;
    hub.label.font = [UIFont systemFontOfSize:14.0];
    hub.mode = MBProgressHUDModeText;
    hub.margin = 15;
    hub.removeFromSuperViewOnHide = YES;
    hub.userInteractionEnabled = ENABLE_UE;
    [self addSubview:hub];
    [hub showAnimated:true];
    [hub hideAnimated:true afterDelay:seconds];
    return hub;
}

- (MBProgressHUD *)showProgressOnlyDetailText:(NSString *)message
{
    [self removeProgressImmediately];
    if(![message isKindOfClass:[NSString class]])
    {
        return [self ShowProgress];
    }
    return [self showProgressOnlyDetailText:message delay:3.0f];
}


- (MBProgressHUD *)showProgressOnlyDetailText:(NSString *)message delay:(float)seconds
{
    MBProgressHUD * hub = [MBProgressHUD HUDForView:self];
    if (!hub) {
        hub = [[MBProgressHUD alloc]initWithView:self];
    }
    hub.mode = MBProgressHUDModeText;
    hub.margin = 15;
    hub.removeFromSuperViewOnHide = YES;
    hub.userInteractionEnabled = ENABLE_UE;
    [self addSubview:hub];
    [hub showAnimated:true];
    [hub hideAnimated:true afterDelay:seconds];
    hub.detailsLabel.text = message;
    hub.detailsLabel.font = [UIFont systemFontOfSize:14.0];
    return hub;
}

- (MBProgressHUD *)showProgressWithTitle:(NSString *)title Detail:(NSString *)detail {
    return [self showProgressWithTitle:title Detail:detail delay:3];
}

- (MBProgressHUD *)showProgressWithTitle:(NSString *)title Detail:(NSString *)detail delay:(float)seconds {
    MBProgressHUD * hub = [MBProgressHUD HUDForView:self];
    if (!hub) {
        hub = [[MBProgressHUD alloc]initWithView:self];
    }
    hub.mode = MBProgressHUDModeText;
    hub.margin = 15;
    hub.removeFromSuperViewOnHide = YES;
    hub.userInteractionEnabled = ENABLE_UE;
    [self addSubview:hub];
    [hub showAnimated:true];
    [hub hideAnimated:true afterDelay:seconds];
    hub.label.text = title;
    hub.detailsLabel.text = detail;
    hub.label.font = [UIFont boldSystemFontOfSize:14.0];
    hub.detailsLabel.font = [UIFont systemFontOfSize:12.0];
    return hub;
}

- (MBProgressHUD *)showProgressWithBlock:(hud_block)block
{
    MBProgressHUD * hud = [MBProgressHUD HUDForView:self];
    if (!hud) {
        hud = [[MBProgressHUD alloc]initWithView:self];
    }
    block(hud);
    hud.removeFromSuperViewOnHide = YES;
    [self addSubview:hud];
    [hud showAnimated:true];
    return hud;
}

- (void)removeProgress
{
    [MBProgressHUD hideHUDForView:self animated:true];
}

- (void)removeProgressImmediately
{
    [MBProgressHUD hideHUDForView:self animated:false];
}

- (void)removeProgressDelegate
{
    MBProgressHUD * hud = [MBProgressHUD HUDForView:self];
    hud.delegate = nil;

}
#pragma clang diagnostic pop
@end
